package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private Teacher[] teachers=new Teacher[5];
    public boolean addTeacher(Teacher teacher) {
        boolean flag=false;
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i]==null){
                teachers[i]=teacher;
                flag=true;
                break;
            }
        }
        return flag;

    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacher(String id) {
        int index=getIndex(id);
        teachers[index]=null;
    }

    private int getIndex(String id) {
        int index=-1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && teachers[i].getId().equals(id))
                index=i;
        }
        return index;
    }

    public void updateTeacher(Teacher teacher, String id) {
        int index=getIndex(id);
        teachers[index]=teacher;
    }
}
